package com.qlslylq.ad.sdk.core.ad;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.kwad.sdk.api.KsFeedAd;
import com.mbridge.msdk.nativex.view.MBMediaView;
import com.mbridge.msdk.out.Campaign;
import com.mbridge.msdk.out.MBNativeHandler;
import com.qlslylq.ad.sdk.enums.AdPlatformEnum;
import com.qq.e.ads.nativ.NativeExpressADView;

import java.util.HashMap;
import java.util.Map;

/**
 * created by qlslylq <br/>
 * on 2022/2/11 <br/>
 * 信息流广告(单个) <br/>
 * 用于展示给app端<br/>
 */
public class SingleFeedAd {

    private static final String KEY_FEED_VIEW = "KEY_FEED_VIEW";
    public static final String KEY_HANDLER = "KEY_HANDLER";

    //广告
    private Object ad;

    //平台
    private AdPlatformEnum platform;

    //其它
    private Map<String, Object> extra;

    public SingleFeedAd(Object ad, AdPlatformEnum platform) {
        this.ad = ad;
        this.platform = platform;
    }

    public SingleFeedAd(Object ad, AdPlatformEnum platform, Map<String, Object> extra) {
        this.ad = ad;
        this.platform = platform;
        this.extra = extra;
    }

    public Object getAd() {
        return ad;
    }

    public void setAd(Object ad) {
        this.ad = ad;
    }

    public AdPlatformEnum getPlatform() {
        return platform;
    }

    public void setPlatform(AdPlatformEnum platform) {
        this.platform = platform;
    }

    public Map<String, Object> getExtra() {
        if (extra == null) {
            extra = new HashMap<>();
        }
        return extra;
    }

    public void setExtra(Map<String, Object> extra) {
        this.extra = extra;
    }

    /**
     * 获取广告视图<br/>
     */
    public View getAdView(Context context) {
        View view = null;
        if (platform == AdPlatformEnum.PLATFORM_CSJ) {
            view = ad == null ? null : ((TTNativeExpressAd) ad).getExpressAdView();
        } else if (platform == AdPlatformEnum.PLATFORM_YLH) {
            view = (NativeExpressADView) ad;
        } else if (platform == AdPlatformEnum.PLATFORM_KS) {
            view = ad == null ? null : ((KsFeedAd) ad).getFeedView(context);
        } else if (platform == AdPlatformEnum.PLATFORM_SIGMOB) {

        } else if (platform == AdPlatformEnum.PLATFORM_MINTEGRAL) {
            view = ad == null ? null : getMBFeedView(context);
        }
        return view;
    }

    private MBMediaView getMBFeedView(Context context) {
        MBMediaView feedView = (MBMediaView) getExtra().get(KEY_FEED_VIEW);
        if (feedView == null) {
            feedView = new MBMediaView(context);
            feedView.setProgressVisibility(true);
            feedView.setSoundIndicatorVisibility(true);
            feedView.setVideoSoundOnOff(true);
            feedView.setFullScreenViewBackgroundColor(0xFFCD5C5C);
            feedView.setFollowActivityOrientation(false);
            extra.put(KEY_FEED_VIEW, feedView);
        }
        feedView.setNativeAd((Campaign) ad);
        return feedView;
    }

    /**
     * 渲染<br/>
     */
    public void render(ViewGroup itemView) {
        if (platform == AdPlatformEnum.PLATFORM_CSJ) {
            TTNativeExpressAd ad = (TTNativeExpressAd) getAd();
            if (ad != null) {
                ad.render();
            }
        } else if (platform == AdPlatformEnum.PLATFORM_YLH) {
            NativeExpressADView ad = (NativeExpressADView) getAd();
            if (ad != null) {
                ad.render();
            }
        } else if (platform == AdPlatformEnum.PLATFORM_KS) {

        } else if (platform == AdPlatformEnum.PLATFORM_SIGMOB) {

        } else if (platform == AdPlatformEnum.PLATFORM_MINTEGRAL) {
            if (ad != null) {
                MBNativeHandler handler = ((MBNativeHandler) getExtra().get(KEY_HANDLER));
                if (handler != null) {
                    handler.registerView(itemView, (Campaign) ad);
                }
            }
        }
    }

}
